(function($) {
    /***   参数说明
     ****   url :  <%=ViewUtil.getRequestPath(request)%>  固定写法
     ****   cptName : 加载的cpt文件名称
     ****   argList : {}, 参数传递
     ****   iframeName : 要加载报表的iframe 的 name 属性值
     ****   isDebug  :  是否为调试模式
     ****
     ***/
    var LantopFineReport = function(url, argList, iframeName, isDebug) {
        this._fineReport = "ReportServer";
        this._action = url + this._fineReport;
        this._iframeName = iframeName;

        this._debug = (isDebug === undefined ? true : isDebug);
        this._argList = argList;

        myForm = $("<form id=lantop_fineReport_hidden_form></form>");
        myForm.attr('action', this._action);
        myForm.attr('method', "POST"); // POST 提交参数
        myForm.attr('target', this._iframeName);

        this._myForm = myForm;

        // 循环添加表单的隐藏属性
        $.each(this._argList, function(name, value) {
            input = $("<input type='hidden' name=" + name + " />");
            input.attr('value', value);
            myForm.append(input);
        });

        myForm.css("display", "block");
        myForm.appendTo("body");

        if (this._debug) {
            //console.log(myForm);
        }
    };

    LantopFineReport.prototype.submit = function(extendArgs) {
        var _myFrom = this._myForm;
        extendArgs = $.extend({}, extendArgs);

        $.each(extendArgs, function(name, value) {

            $("input").remove("input[name='" + name + "']");
        });

        //$("input").remove("input[name='SQL']");
        //$("input").remove("input[name='reportlet']");

        $.each(extendArgs, function(name, value) {

            input = $("<input type='hidden' name=" + name + " />");
            input.attr('value', cjkEncode(value));

            _myFrom.append(input);
        });

        _myFrom.submit();
    };



    function cjkEncode(text) {
        if (text == null) {
            return "";
        }
        var newText = "";
        for (var i = 0; i < text.length; i++) {
            var code = text.charCodeAt(i);
            if (code >= 128 || code == 91 || code == 93) { //91 is "[", 93 is "]".
                newText += "[" + code.toString(16) + "]";
            } else {
                newText += text.charAt(i);
            }
        }
        return newText;
    }



    window.LantopFineReport = LantopFineReport;

})($);